#include <QtGui>
#include "InfoWindow.h"
#include "GameEngine.h"
#include "Player.h"

InfoWindow::InfoWindow(QWidget *parent)
	: QWidget(parent)
{
	colorP1 = new QLabel( "" );
	colorP2 = new QLabel( "" );
	scoreP1Label = new QLabel( tr("Score:") );
	scoreP2Label = new QLabel( tr("Score:") );
	movesP1Label = new QLabel( tr("Moves:") );
	movesP2Label = new QLabel( tr("Moves:") );
	nameP1 = new QLabel( "" );
	nameP2 = new QLabel( "" );
	scoreP1 = new QLabel( "" );
	scoreP2 = new QLabel( "" );
	movesP1 = new QLabel( "" );
	movesP2 = new QLabel( "" );
	typeP1 = new QLabel("( )");
	typeP2 = new QLabel("( )");
	movesListLabel = new QLabel( tr("<b>List of played moves</b>") );
	
	listMoves = new QTextEdit( "Empty" );
	listMoves->setReadOnly(true);
	
	infosGame = new QTextEdit( "" );
	infosGame->setReadOnly(true);

	topLayout = new QGridLayout;
	topLayout->addWidget(colorP1,0,0);
	topLayout->addWidget(nameP1,0,2);
	topLayout->addWidget(typeP1,1,2);
	topLayout->addWidget(scoreP1Label,2,1);
	topLayout->addWidget(scoreP1,2,2);
	topLayout->addWidget(movesP1Label,3,1);
	topLayout->addWidget(movesP1,3,2);
	
	topLayout->addWidget(colorP2,5,0);
	topLayout->addWidget(nameP2,5,2);
	topLayout->addWidget(typeP2,6,2);
	topLayout->addWidget(scoreP2Label,7,1);
	topLayout->addWidget(scoreP2,7,2);
	topLayout->addWidget(movesP2Label,8,1);
	topLayout->addWidget(movesP2,8,2);
	
	toptopLayout = new QHBoxLayout;
	toptopLayout->addLayout(topLayout);
	toptopLayout->addStretch();
	
	centerLayout = new QHBoxLayout;
	centerLayout->addStretch();
	centerLayout->addWidget(movesListLabel);
	centerLayout->addStretch();
	
	mainLayout = new QVBoxLayout;
	mainLayout->addLayout(toptopLayout);
	mainLayout->addStretch();
	mainLayout->addWidget(infosGame);
	mainLayout->addStretch();
	mainLayout->addLayout(centerLayout);
	mainLayout->addWidget(listMoves);
	
	this->setLayout(mainLayout);
	
	this->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Preferred);
		
}


InfoWindow::~InfoWindow()
{
//	delete 
}


void InfoWindow::setColorP1( ColorPawn c )
{
	if( c == Black )
	{
		colorP1->setText( QString(tr("<b>Black :</b>")) );
	}
	else
	{
		colorP1->setText( QString(tr("<b>White :</b>")) );
	}
}

void InfoWindow::setColorP2( ColorPawn c )
{
	if( c == Black )
	{
		colorP2->setText( QString(tr("<b>Black :</b>")) );
	}
	else
	{
		colorP2->setText( QString(tr("<b>White :</b>")) );
	}
}

void InfoWindow::setNameP1( QString name )
{
	QString str = "<b>" + name + "</b>";
	nameP1->setText(str);
}

void InfoWindow::setNameP2( QString name )
{
	QString str = "<b>" + name + "</b>";
	nameP2->setText(str);
}

void InfoWindow::setTypeP1( TypePlayer t )
{
	switch( t )
	{
		case Local:
			typeP1->setText( QString(tr("( Human Local )")) );
			break;
		case Distant:
			typeP1->setText( QString(tr("( Human Distant )")) );
			break;
		case AI_1:
			typeP1->setText( QString(tr("( Computer Level 1 )")) );
			break;
		case AI_2:
			typeP1->setText( QString(tr("( Computer Level 2 )")) );
			break;
		case AI_3:
			typeP1->setText( QString(tr("( Computer Level 3 )")) );
			break;
		default:
			typeP1->setText( QString(tr("( )")) );
			break;
	}
}

void InfoWindow::setTypeP2( TypePlayer t )
{
	switch( t )
	{
		case Local:
			typeP2->setText( QString(tr("( Human Local )")) );
			break;
		case Distant:
			typeP2->setText( QString(tr("( Human Distant )")) );
			break;
		case AI_1:
			typeP2->setText( QString(tr("( Computer Level 1 )")) );
			break;
		case AI_2:
			typeP2->setText( QString(tr("( Computer Level 2 )")) );
			break;
		case AI_3:
			typeP2->setText( QString(tr("( Computer Level 3 )")) );
			break;
		default:
			typeP2->setText( QString(tr("( )")) );
			break;
	}
}

void InfoWindow::setScoreP1( int score )
{
	scoreP1->setNum( score );
}

void InfoWindow::setScoreP2( int score )
{
	scoreP2->setNum( score );
}

void InfoWindow::setMovesP1( int moves )
{
	movesP1->setNum( moves );
}

void InfoWindow::setMovesP2( int moves )
{
	movesP2->setNum( moves);
}

void InfoWindow::setListMoves( QString list )
{
	listMoves->setText( list );
}

void InfoWindow::connecting( GameEngine* ge )
{
	playerTable = ge->getPlayer();
	connect( ge, SIGNAL( playerNameModified(Player*) ), this, SLOT( updatingName(Player*) ) );
	connect( ge, SIGNAL( playerScoreModified() ), this, SLOT( updatingScore() ) );
	connect( ge, SIGNAL( playerNumMovesModified() ), this, SLOT( updatingMoves() ) );
	connect( ge, SIGNAL( playerColorModified(Player*) ), this, SLOT( updatingColor(Player*) ) );
	connect( ge, SIGNAL( playerTypeModified(Player*) ), this, SLOT( updatingType(Player*) ) );

	connect( ge, SIGNAL( movesListModified(QString) ), this, SLOT( setListMoves(QString) ) );
	connect( ge, SIGNAL( sendInfosGameDisplay(IdMsgInfos) ), this, SLOT( setInfosGame(IdMsgInfos) ) );
}

void InfoWindow::updatingColor( Player *p )
{
//	cout << "Entre ds \"InfoWindow::updatingColor(Player*)\"" << endl;
	
	if( p->getId() == p1 )
	{
		setColorP1( p->getColor() );
	}
	else
	{
		setColorP2( p->getColor() );
	}
}

void InfoWindow::updatingType( Player *p )
{
//	cout << "Entre ds \"InfoWindow::updatingType(Player*)\"" << endl;
	
	if( p->getId() == p1 )
	{
		setTypeP1( p->getType() );
	}
	else
	{
		setTypeP2( p->getType() );
	}	
}

void InfoWindow::updatingName( Player *p )
{
//	cout << "Entre ds \"InfoWindow::updatingName(Player*)\"" << endl;
	
	if( p->getId() == p1 )
	{
		setNameP1( p->getName() );
	}
	else
	{
		setNameP2( p->getName() );
	}
}

void InfoWindow::updatingScore()
{
	setScoreP1( playerTable[p1]->getScore() );
	setScoreP2( playerTable[p2]->getScore() );
}

void InfoWindow::updatingMoves()
{
	setMovesP1( playerTable[p1]->getNumMoves() );
	setMovesP2( playerTable[p2]->getNumMoves() );
}

void InfoWindow::setInfosGame(IdMsgInfos idMsg)
{
	QString msg;
	switch( idMsg )
	{
		case P1_Playing:
			msg = playerTable[p1]->getName() + " ( ";
			if( playerTable[p1]->getColor() == Black )
			{
				msg += "black";
			}
			else
			{
				msg += "white";
			}
			msg += " pawns ) is playing ...";			
			break;
		
		case P2_Playing:
			msg = playerTable[p2]->getName() + " ( ";
			if( playerTable[p2]->getColor() == Black )
			{
				msg += "black";
			}
			else
			{
				msg += "white";
			}
			msg += " pawns ) is playing ...";			
			break;
		
		default:
			break;
	}
	
	infosGame->setText(msg);
}

void InfoWindow::writeSettings()
{
	QSettings settings("Maurryson Inc.", "QthelloMania");
	settings.setValue( "name_p1", playerTable[p1]->getName() );
	settings.setValue( "name_p2", playerTable[p2]->getName() );
	switch( playerTable[p1]->getType() )
	{
		case AI_1:
			settings.setValue( "type_p1", 1 );
			break;
		case Local:
		default:
			settings.setValue( "type_p1", 0 );
			break;
	}
	switch( playerTable[p2]->getType() )
	{
		case AI_1:
			settings.setValue( "type_p2", 1 );
			break;
		case Local:
		default:
			settings.setValue( "type_p2", 0 );
			break;
	}
}

void InfoWindow::readSettings()
{
	QSettings settings("Maurryson Inc.", "QthelloMania");
	playerTable[p1]->setName( settings.value("name_p1", tr("Player 1")).toString() );
	playerTable[p2]->setName( settings.value("name_p2", tr("Player 2")).toString() );

//	TypePlayer type_p1, type_p2;
	switch( settings.value("type_p1", 0).toInt() )
	{
		case 1:
			playerTable[p1]->setType( AI_1 );
			break;
		case 0:
		default:
			playerTable[p1]->setType( Local );
			break;
	}
	switch( settings.value("type_p2", 0).toInt() )
	{
		case 1:
			playerTable[p2]->setType( AI_1 );
			break;
		case 0:
		default:
			playerTable[p2]->setType( Local );
			break;
	} 
}


Player* InfoWindow::getPlayer( int id )
{
	return playerTable[id];
}
